package com.chance.cc.crawler.prod.command.job.domain.news.sina.module;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.queue.crawler.CrawlerQueueConfig;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.news.sina.SinaCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.dateRange;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.keyOrDateRange;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Site_Info;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/4/8 19:05
 * @Description
 *      新浪板块 新媒体 实时
 **/
public class SinaMpRealtimeCrawlerSchedulerJob extends SinaCommonCrawlerSchedulerJob {
    private static final String crawler_level = "realtime";

    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
//        System.out.println(crawlerSchedulejob().generateCrawlerTriggerKey());
    }

    public static CrawlerJob publishCrawlerScheduleJobInfo(){
        CrawlerJob crawlerJob = crawlerSchedulejob();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("新浪体育新媒体10分钟定时采集");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler作业：" + page.getRawText());
        return crawlerJob;

    }

    public static CrawlerJob crawlerSchedulejob(){
        String url = "https://interface.sina.cn/auto/news/getWapNewsNewBycID.d.json?cid=178994&page=1&limit=20&tagid=0";
        String site = "auto";
        String siteInfo = "mp";
        String siteBiz = "news-" + crawler_level;//正式上线之后再加,真正的site_biz = news-realtime
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPage)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(keyOrDateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,siteInfo,siteBiz,"queue")))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(2,null))
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .proxy(proxy)
                .build();
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,siteInfo);
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("syc","true");//进行同步数据

        CrawlerRecord commentFilter = new CrawlerRequestRecord();
        commentFilter.setFilter(dateRange);
//        commentFilter.addFilterInfo(FilterUtils.memoryFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,"queue")));
        commentFilter.addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null));
        crawlerRequestRecord.tagsCreator().resultTags().getCategoryTag().addKVTag("comment_filter_record", JSON.toJSONString(commentFilter));

        //每条帖子的结果
        String resultPostQueueName = "sina_mp_item_url";
        CrawlerQueueConfig crawlerPostQueueConfig = new CrawlerQueueConfig(
                resultPostQueueName,
                CrawlerQueueConfig.Content.result,
                CrawlerQueueConfig.Storage.redis,
                CrawlerQueueConfig.Structure.list);
        //每一条帖子结果同步
        CrawlerRequestRecord syncCrawlerPostRecord = CrawlerRequestRecord.builder()
                .startPageRequest("sina_post_result", turnPageItem)
                .httpUrl("http://"+metaServiceIp+":"+metaServicePort+"/crawler/domain/common/api/v1/"+domain+"/search/results/sync?resultQueue=" + resultPostQueueName + "&site=sina_mp_item_url")
                .requestLabelTag(supportCallback)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,siteInfo,siteBiz,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain,site,siteInfo,siteBiz,"queue")))
                .queueResultPipeline("mysql",crawlerPostQueueConfig)
                .kafkaResultPipeline("kafka",kafkaTopic,null)
                .requestRecord(crawlerRequestRecord)
                .supportRecord(syncCrawlerPostRecord)
                .build();
    }
}
